package com.java83.service.impl;

import com.java83.dao.RbacManagerDao;
import com.java83.entity.RbacManager;
import com.java83.service.RbacManagerService;
import com.java83.utils.JWTUtils;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * (RbacManager)表服务实现类
 *
 * @author linzihao
 * @since 2021-12-07 20:04:46
 */
@Service("rbacManagerService")
public class RbacManagerServiceImpl implements RbacManagerService {
    @Resource
    private RbacManagerDao rbacManagerDao;
    @Autowired
    RedisTemplate redisTemplate;
    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public RbacManager queryById(Integer id) {
        return this.rbacManagerDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param rbacManager 筛选条件
     * @return 查询结果
     */
    @Override
    public List<RbacManager> queryByPage(RbacManager rbacManager) {
        return new ArrayList<RbacManager>(this.rbacManagerDao.queryAllByLimit(rbacManager));
    }

    /**
     * 新增数据
     *
     * @param rbacManager 实例对象
     * @return 实例对象
     */
    @Override
    public int insert(RbacManager rbacManager) {
        //提供，新增用户时候的，密码加密功能！
        String password = rbacManager.getPassword();
        //就是一个加密算法，别人写好的，中间的password，是目标被加密的对象，最后的一个参数是salt 盐 (加密结果收到盐的影响)
        SimpleHash simpleHash = new SimpleHash("MD5", password, rbacManager.getAccount()+"java83");
        String newPassword = simpleHash.toHex()+"ayu";//转成16进制字符串
        rbacManager.setPassword(newPassword);
        return rbacManagerDao.insert(rbacManager);
    }

    /**
     * 修改数据
     *
     * @param rbacManager 实例对象
     * @return 实例对象
     */
    @Override
    public RbacManager update(RbacManager rbacManager) {
        this.rbacManagerDao.update(rbacManager);
        return this.queryById(rbacManager.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public int deleteById(Integer id) {
        return this.rbacManagerDao.deleteById(id);
    }

    @Override
    public RbacManager login(String username, String password) {
        RbacManager rbacManager = new RbacManager();
        rbacManager.setAccount(username);
        //就是一个加密算法，别人写好的，中间的password，是目标被加密的对象，最后的一个参数是salt 盐 (加密结果收到盐的影响)
        SimpleHash simpleHash = new SimpleHash("MD5", password, rbacManager.getAccount()+"java83");
        String newPassword = simpleHash.toHex()+"ayu";//转成16进制字符串
        rbacManager.setPassword(newPassword);
        RbacManager login = rbacManagerDao.login(rbacManager);
        //在这个RbacManager对象中封装token
        String jwt = JWTUtils.sign(username, newPassword);
        redisTemplate.opsForValue().set("userToken",login);
        redisTemplate.expire("myspringboothash",10000, TimeUnit.SECONDS);
        login.setToken(jwt);

        return login;
    }

    @Override
    public RbacManager loginAuth(String username, String password) {
        RbacManager rbacManager = new RbacManager();
        rbacManager.setAccount(username);
        rbacManager.setPassword(password);
        RbacManager rbacManager2 = rbacManagerDao.login(rbacManager);
        return rbacManager2;
    }
}
